package com.mbt.remote.server.event;

import java.awt.Robot;
import java.awt.event.KeyEvent;

/**
 * @author test
 */
public class UserInterface implements Runnable {

	String key = "";
	private int count1 = -1;
	private int count2 = -1;
	private int count3 = -1;
	private int count4 = -1;
	private int count5 = -1;
	private int count6 = -1;
	private int count7 = -1;
	private int count8 = -1;
	private int count9 = -1;
	private int count0 = -1;
	Thread t;
	private Robot robot;

	public UserInterface() {
	}

	public UserInterface(Robot robot) {
		this.robot = robot;
	}

	private void sentKey(int keycode) {
		System.out.println("KEYPRESSED : " + getKeyName(keycode));
		robot.keyPress(keycode);
		robot.keyRelease(keycode);
	}

	private void sentKeyWithBackSpace(int keycode) {
		sentKey(KeyEvent.VK_BACK_SPACE);
		sentKey(keycode);
	}

	public void keyPressed(int keyCode) {
		key = getKeyName(keyCode);
		if (getKeyName(keyCode).equals("1")) {
			count1++;
			if (count1 == 0) {
				t = new Thread(this);
				t.start();
				sentKey(KeyEvent.VK_1);
			} else if (count1 == 1 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_BACK_SPACE);
			}
		}

		if (getKeyName(keyCode).equals("2")) {
			count2++;
			if (count2 == 0) {
				t = new Thread(this);
				t.start();
				sentKey(KeyEvent.VK_2);
			} else if (count2 == 1 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_A);
			} else if (count2 == 2 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_B);
			} else if (count2 == 3 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_C);
			}
		}
		if (getKeyName(keyCode).equals("3")) {
			count3++;
			if (count3 == 0) {
				t = new Thread(this);
				t.start();
				sentKey(KeyEvent.VK_3);
			} else if (count3 == 1 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_D);
			} else if (count3 == 2 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_E);
			} else if (count3 == 3 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_F);
			}
		}
		if (getKeyName(keyCode).equals("4")) {
			count4++;
			if (count4 == 0) {
				t = new Thread(this);
				t.start();
				sentKey(KeyEvent.VK_4);
			} else if (count4 == 1 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_G);
			} else if (count4 == 2 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_H);
			} else if (count4 == 3 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_I);
			}
		}
		if (getKeyName(keyCode).equals("5")) {
			count5++;
			if (count5 == 0) {
				t = new Thread(this);
				t.start();
				sentKey(KeyEvent.VK_5);
			} else if (count5 == 1 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_J);
			} else if (count5 == 2 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_K);
			} else if (count5 == 3 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_L);
			}
		}
		if (getKeyName(keyCode).equals("6")) {
			count6++;
			if (count6 == 0) {
				t = new Thread(this);
				t.start();
				sentKey(KeyEvent.VK_6);
			} else if (count6 == 1 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_M);
			} else if (count6 == 2 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_N);
			} else if (count6 == 3 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_O);
			}
		}
		if (getKeyName(keyCode).equals("7")) {
			count7++;
			if (count7 == 0) {
				t = new Thread(this);
				t.start();
				sentKey(KeyEvent.VK_7);
			} else if (count7 == 1 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_P);
			} else if (count7 == 2 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_Q);
			} else if (count7 == 3 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_R);
			} else if (count7 == 4 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_S);
			}
		}
		if (getKeyName(keyCode).equals("8")) {
			count8++;
			if (count8 == 0) {
				t = new Thread(this);
				t.start();
				sentKey(KeyEvent.VK_8);
			} else if (count8 == 1 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_T);
			} else if (count8 == 2 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_U);
			} else if (count8 == 3 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_V);
			}
		}
		if (getKeyName(keyCode).equals("9")) {
			count9++;
			if (count9 == 0) {
				t = new Thread(this);
				t.start();
				sentKey(KeyEvent.VK_9);
			} else if (count9 == 1 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_W);
			} else if (count9 == 2 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_X);
			} else if (count9 == 3 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_Y);
			} else if (count9 == 4 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_Z);
			}
		}
		if (getKeyName(keyCode).equals("0")) {
			count0++;
			if (count0 == 0) {
				t = new Thread(this);
				t.start();
				sentKey(KeyEvent.VK_0);
			} else if (count0 == 1 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_SPACE);
			} else if (count0 == 2 && t.isAlive() == true) {
				sentKeyWithBackSpace(KeyEvent.VK_CAPS_LOCK);
			}
		}
	}

	private String getKeyName(int keyCode) {

		return "" + (keyCode - 48);
	}

	public void run() {
		if (key.equals("0")) {
			try {
				Thread.sleep(1500);
				count0 = -1;
			} catch (Exception e) {
			}
		}
		if (key.equals("1")) {
			try {
				Thread.sleep(800);
				count1 = -1;
			} catch (Exception e) {
			}
		}
		if (key.equals("2")) {
			try {
				Thread.sleep(1600);
				count2 = -1;
			} catch (Exception e) {
			}
		}
		if (key.equals("3")) {
			try {
				Thread.sleep(1600);
				count3 = -1;
			} catch (Exception e) {
			}
		}
		if (key.equals("4")) {
			try {
				Thread.sleep(1600);
				count4 = -1;
			} catch (Exception e) {
			}
		}
		if (key.equals("5")) {
			try {
				Thread.sleep(1600);
				count5 = -1;
			} catch (Exception e) {
			}
		}
		if (key.equals("6")) {
			try {
				Thread.sleep(1600);
				count6 = -1;
			} catch (Exception e) {
			}
		}
		if (key.equals("7")) {
			try {
				Thread.sleep(1600);
				count7 = -1;
			} catch (Exception e) {
			}
		}
		if (key.equals("8")) {
			try {
				Thread.sleep(1600);
				count8 = -1;
			} catch (Exception e) {
			}
		}
		if (key.equals("9")) {
			try {
				Thread.sleep(1600);
				count9 = -1;
			} catch (Exception e) {
			}
		}
	}
}
